﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Compilation;

namespace MyClub.Web.UI.WebControls
{
    /// <summary>
    /// Defines the properties of a case in the <see cref="SwitchView"/> control.
    /// </summary>
    [ParseChildren(true)]
    public class SwitchViewCase: StateManagedObject
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SwitchViewCase"/> class.
        /// </summary>
        public SwitchViewCase()
        {
        }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        [PersistenceMode(PersistenceMode.Attribute)]
        public string Value 
        {
            get { return (string)ViewState["Value"]; }
            set { ViewState["Value"] = value; }
        }

        /// <summary>
        /// Gets or sets the case template file.
        /// </summary>
        /// <value>The case template file.</value>
        [UrlProperty("*.ascx")]
        public string CaseTemplateFile
        {
            get { return (string)ViewState["CaseTemplateFile"]; }
            set { ViewState["CaseTemplateFile"] = value; }
        }

        /// <summary>
        /// Gets or sets the case template.
        /// </summary>
        /// <value>The case template.</value>
        [TemplateInstance(TemplateInstance.Multiple)]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public ITemplate CaseTemplate 
        { 
            get; 
            set; 
        }

        /// <summary>
        /// Gets the template.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <returns></returns>
        internal ITemplate GetTemplate(Control container)
        {
            if (container == null)
                throw new ArgumentNullException("container");

            if (CaseTemplate != null)
                return CaseTemplate;

            if (!string.IsNullOrEmpty(CaseTemplateFile))
                return container.Page.LoadTemplate(CaseTemplateFile);

            return null;
        }
    }
}
